<template>
  <!-- 注册页面 -->
  <div>
    <van-nav-bar
      title="账号注册"
      left-arrow
      @click-left="onClickLeft"
      class="heard"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{6,}$/, message: '用户名至少包含6个字符' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码必须包含6个字符' }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <p @click="$router.push('/login')">已有账号？去登录~</p>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'register-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      const data = await register(values)
      console.log(data)
      this.$toast.success('注册成功')
      this.$router.push('/login')
    },
    onClickLeft () {
      this.$router.push('my')
    }
  }
}
</script>

<style scoped lang="less">
p {
  color: #464545;
  font-size: 14px;
  text-align: center;
  margin-top: 10px;
}
</style>
